package com.shxc.smarttrade.old.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.MessageBean;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/11/7.
 */

public class MessageDetailActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_content)
    TextView tvContent;
    private String msgId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_detail);
        initView();
        haveReadMsg();
    }

    private void initView() {
        Bundle bundle = getIntent().getExtras();
        MessageBean.DataBean db = (MessageBean.DataBean) bundle.getSerializable("message");
        if (db.getMsgType() == 0)
            tvCenter.setText("系统消息");
        else if (db.getMsgType() == 1)
            tvCenter.setText("交易消息");
        tvTitle.setText(db.getMsgTitle());
        tvContent.setText(db.getMsgInfo());
        tvTime.setText(BaseUtils.getDateFromMillionSeconds(db.getCreatedAt(), "yyyy-MM-dd HH:mm:ss"));
        msgId = String.valueOf(db.getId());

    }

    @OnClick(R.id.rl_left)
    public void onViewClicked() {
        setResult(111);
        finish();
    }

    private void haveReadMsg() {
        Map<String, Object> map = new HashMap<>();
        map.put("msgId", msgId);
        map.put("accessToken", accessTokenss);
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.MESSAGE_DETAIL_HAVE_READ, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast("网络异常");
            }

            @Override
            public void success(String response, int id) {

                JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                String errCode = "";
                String errMsg = "";
                if (!jsonObject.get("err_code").isJsonNull())
                    errCode = jsonObject.get("err_code").getAsString();
                if (!jsonObject.get("err_msg").isJsonNull())
                    errMsg = jsonObject.get("err_msg").getAsString();
                if (!errCode.equals("0"))
                    showToast(errMsg);

            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(111);
        finish();
    }
}
